package com.xyrl.project.app.controller.csr;

import com.xyrl.common.base.exception.XyrlException;
import com.xyrl.common.util.ObjectUtil;
import com.xyrl.common.util.domain.RegexpConstant;
import com.xyrl.project.app.util.ShiroUtil;
import com.xyrl.project.common.annotation.Log;
import com.xyrl.project.common.base.vo.ResponseData;
import com.xyrl.project.common.csr.service.CsrUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.regex.Matcher;

@RestController
@RequestMapping("/csr/user")
@Api(tags = "客户信息")
public class CsrUserController {
    @Autowired
    private CsrUserService csrUserService;

    /**
     * 客户设置交易密码
     *
     * @param storeId 商户id
     * @param pwd     交易密码
     * @author zhongp
     * @date 2020-01-08 16:24:22
     */
    @Log("客户信息-设置交易密码")
    @ApiOperation(value = "客户信息-设置交易密码")
    @PostMapping("/setTradePwd")
    public ResponseData setTradePwd(Long storeId, String pwd) {
        try {
            checkPwd(pwd);
            csrUserService.setTradePwd(storeId, ShiroUtil.loginUserId(), pwd);
        } catch (XyrlException e) {
            return ResponseData.error(e.getMessage());
        }
        return ResponseData.success("设置交易密码成功");
    }

    @Log("客户信息-密码校验")
    @ApiOperation(value = "客户信息-密码校验")
    @PostMapping("/checkTradePwd")
    public ResponseData checkTradePwd(Long storeId, String pwd) {
        try {
            checkPwd(pwd);
            csrUserService.checkTradePwd(storeId, ShiroUtil.loginUserId(), pwd);
        } catch (XyrlException e) {
            return ResponseData.error(e.getMessage());
        }
        return ResponseData.success("设置交易密码成功");
    }

    /**
     * 修改用户交易密码
     *
     * @param storeId 商户id
     * @param oldPwd  原始交易密码
     * @param newPwd  新交易密码
     * @author zhongp
     * @date 2020-01-08 16:26:14
     */
    @Log("客户信息-修改交易密码")
    @ApiOperation(value = "客户信息-修改交易密码")
    @PostMapping("/updateTradePwd")
    public ResponseData updateTradePwd(Long storeId, String oldPwd, String newPwd) {
        try {
            checkPwd(oldPwd);
            checkPwd(newPwd);
            csrUserService.updateTradePwd(storeId, ShiroUtil.loginUserId(), oldPwd, newPwd);
        } catch (XyrlException e) {
            return ResponseData.error(e.getMessage());
        }
        return ResponseData.success("修改交易密码成功");
    }

    /**
     * 身份认证
     *
     * @param attIds
     * @return
     */
    @Log("客户信息-实名认证")
    @ApiOperation(value = "客户信息-实名认证")
    @PostMapping("/personVerify")
    public ResponseData personVerify(Long storeId, Long[] attIds) {
        try {
            String verifyId=csrUserService.personVerify(storeId, attIds);
            return ResponseData.success(verifyId);
        } catch (XyrlException e) {
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 找回交易密码
     *
     * @param pwd
     * @return
     */
    @Log("客户信息-找回交易密码")
    @ApiOperation(value = "客户信息-找回交易密码")
    @PostMapping("/findTradePwd")
    public ResponseData findTradePwd(Long storeId, String pwd,
                                     @NotBlank(message = "认证id不能为空") String verifyId) {
        try {
            checkPwd(pwd);
            csrUserService.findTradePwd(storeId, ShiroUtil.loginUserId(), pwd, verifyId);
        } catch (XyrlException e) {
            return ResponseData.error(e.getMessage());
        }
        return ResponseData.success("找回交易密码成功");
    }

    private void checkPwd(String pwd) throws XyrlException {
        if (ObjectUtil.isEmpty(pwd)) {
            throw new XyrlException("交易密码不能为空");
        }
        java.util.regex.Pattern r = java.util.regex.Pattern.compile(RegexpConstant.TRADE_PWD_REG);
        Matcher m = r.matcher(pwd);
        if (!m.matches()) {
            throw new XyrlException("交易密码只能由6位数字组成");
        }
    }
}
